﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using YouHP.Model;
using System.Collections.Generic;
using YouHP.BLL;
using System.Text;
using System.IO;
using YouHP.Utility;
using web.Properties;

namespace web.admin
{
    public partial class ProvinceList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        
            List<DistrictInfo> list = null;

            list = DistrictBLL.Instance.GetDistrictListByParent(0);

            this.provinceList.DataSource = list;
            this.provinceList.DataBind();
      

        }
        protected void DeleteBtn_Click(object sender, EventArgs e)
        {
            try
            {
                int[] checkList = new int[this.provinceList.Rows.Count];
                int count = 0;
                for (int rowIndex = 0; rowIndex < this.provinceList.Rows.Count; rowIndex++)
                {
                    bool isCheck = ((CheckBox)this.provinceList.Rows[rowIndex].Cells[0].FindControl("chk")).Checked;
                    if (isCheck)
                    {
                        count++;
                        checkList[rowIndex] = Convert.ToInt32(this.provinceList.DataKeys[rowIndex].Value);
                    }
                }

                int[] idList = new int[count];
                for (int i = 0, j = 0; i < checkList.Length; i++)
                {
                    if (checkList[i] > 0)
                        idList[j++] = checkList[i];
                }

                DistrictBLL.Instance.DelDistrict(idList);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }


        protected void pageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.provinceList.PageIndex = e.NewPageIndex;

          
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            List<DistrictInfo> plist = null;

            plist = DistrictBLL.Instance.GetDistrictListByParent(0);

            StringBuilder sb = new StringBuilder();
            sb.Append("var cityArr = [];\r\n");

            foreach (DistrictInfo item in plist)
            {
                sb.AppendFormat("cityArr[{0}] = [ \r\n", item.Id);
                List<DistrictInfo> clist = DistrictBLL.Instance.GetDistrictListByParent(item.Id);
                int idx = 1;
                foreach (DistrictInfo info in clist)
                {
                    sb.AppendFormat("{{txt:'{0}', val:'{1}'}}", info.Name, info.Id);

                    if (idx == clist.Count) sb.Append("\r\n");
                    else sb.Append(",\r\n");
                    idx++;
                }
                sb.AppendFormat("]; \r\n\r\n");
            }

            sb.Append(Properties.Resources.CitySelector);  

            string path = Utils.GetMapPath("~/support/youhp/district.js");
            StreamWriter writter = new StreamWriter(path,false,Encoding.UTF8);
            writter.Write(sb.ToString());
            writter.Close();

        }

 
    }
}
